<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Teacher</title>
    <link rel="stylesheet" href="//unpkg.com/element-plus/dist/index.css"/>
    <!--引用vue-->
    <script src="https://unpkg.com/vue@3.4.32/dist/vue.global.js"></script>
    <!--引用axios-->
    <script src="https://cdn.jsdelivr.net/npm/axios@0.24.0/dist/axios.min.js"></script>
    <!--引用element-plus-->
    <script src="https://unpkg.com/element-plus/dist/index.full.js"></script>
    <!-- 引用 Element Plus Icons Vue -->
    <script src="https://unpkg.com/@element-plus/icons-vue"></script>
    <script src="/js/axios.min.js"></script>
    <script src="/js/axios-helper.js"></script>
    <style>
        /* 自定义样式 */
        .el-table th, .el-table td {
            text-align: center;
        }
        .el-button--danger {
            background-color: #ff4d4f;
            border-color: #ff4d4f;
        }
    </style>
</head>
<body>
<div id="app">
    <el-container>
        <el-header>
            <h2>教师列表</h2>
        </el-header>
        <el-main>
            <el-table
                    :data="teacherList"
                    style="width: 100%"
                    v-loading="loading"
                    border
                    :header-cell-style="{ background: '#f5f7fa', color: '#333' }"
            >
                <el-table-column prop="id" label="序号" width="80"></el-table-column>
                <el-table-column prop="name" label="名字"></el-table-column>
                <el-table-column prop="subject" label="科目"></el-table-column>
                <el-table-column prop="sid" label="学生编号" width="100"></el-table-column>
                <el-table-column label="操作" width="180">
                    <template #default="scope">
                        <el-button
                                size="small"
                                type="danger"
                                @click="deleteTeacher(scope.row.id)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <el-pagination
                    style="margin-top: 20px; text-align: right;"
                    background
                    layout="total, prev, pager, next, jumper"
                    :total="total"
                    :page-size="pageSize"
                    :current-page="currentPage"
                    @current-change="fetchTeacherList"
            ></el-pagination>
        </el-main>
    </el-container>
</div>
<script>
    const { createApp, onMounted, ref } = Vue;
    const app = createApp({
        setup() {
            const teacherList = ref([]); // 教师列表
            const loading = ref(false); // 加载状态
            const currentPage = ref(1); // 当前页码
            const pageSize = ref(10); // 每页大小
            const total = ref(0); // 总记录数

            // 获取教师列表
            const fetchTeacherList = async (page = 1) => {
                loading.value = true;
                try {
                    const response = await axios.get('/teacher/list', {
                        params: {
                            page: page,
                            size: pageSize.value
                        }
                    });
                    console.log("数据",response);
                    // 使用响应拦截器处理后的数据
                    teacherList.value = response.data.records;
                    total.value = response.data.total;
                    currentPage.value = page;
                } catch (error) {
                    ElMessage.error(`Error fetching teacher data: ${error}`);
                } finally {
                    loading.value = false;
                }
            };
            const deleteTeacher =  (id) => {
                ElementPlus.ElMessageBox.confirm(
                    `此操作将永久删除此教师信息, 是否继续?`,
                    '警告',
                    {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                    }
                )
                    .then(() => {
                        return axios.delete(`/teacher/${id}`)
                    })
                    .then(() => {
                        ElMessage.success('Deleted successfully');
                        fetchTeacherList(currentPage.value);
                    })
                    .catch(errorMsg => ElementPlus.ElMessage({type: 'error', message: 'Failed to delete', showClose: true}))
            };
            // 删除教师
            // const deleteTeacher = async (id) => {
            //     try {
            //         await ElMessageBox.confirm(
            //             'Are you sure you want to delete this teacher?',
            //             'Warning',
            //             {
            //                 confirmButtonText: 'Yes',
            //                 cancelButtonText: 'No',
            //                 type: 'warning',
            //             }
            //         );
            //         const response = await axios.delete(`/teacher/${id}`);
            //         // 使用响应拦截器处理后的数据
            //         ElMessage.success('Deleted successfully');
            //         fetchTeacherList(currentPage.value);
            //     } catch (error) {
            //         ElMessage.error(`Failed to delete: ${error}`);
            //     }
            // };


            // 页面加载时获取数据
            onMounted(() => {
                fetchTeacherList();
            });

            return {
                teacherList,
                loading,
                currentPage,
                pageSize,
                total,
                fetchTeacherList,
                deleteTeacher,
            };
        }
    });

    app.use(ElementPlus);
    for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
        app.component(key, component);
    }
    app.mount('#app');
</script>
</body>
</html>